package com.ipharma.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.dto.Product;

/**
 * A Swing-based dialog class.
 * <P>
 * @author JorgE.H
 */
public class GUIQuantifier extends GUIDialog
{
  /**
   * The layout used by the main panel.
   */
//  BorderLayout layoutBase = new BorderLayout();
  GridBagLayout layoutBase = new GridBagLayout();
  GridLayout layoutQties = new GridLayout( 3, 3, 4, 4 );
  GridLayout layoutOptions = new GridLayout( 1, 2, 0, 0 );

  /**
   * The main panel.
   */
  JPanel jPanelBase = new JPanel();
  JPanel jPanelQties = new JPanel();
  JPanel jPanelOptions = new JPanel();
//  JPanel jPanelTitle = new JPanel();
  JLabel jLabelOptions = new JLabel();
  JLabel jLabelTitle = new JLabel();
  JLabel jLabelQty = new JLabel();
  JLabel jLabelConf = new JLabel();
  GUIButtonImage jButtAcept;
  GUIButtonImage jButtCancel;
  GUIButtonImage jButton1;
  GUIButtonImage jButton2;
  GUIButtonImage jButton3;
  GUIButtonImage jButton4;
  GUIButtonImage jButton5;
  GUIButtonImage jButton6;
  GUIButtonImage jButton7;
  GUIButtonImage jButton8;
  GUIButtonImage jButton9;

  /**
   * Attributes
   */
  Product product;
  String quantity = "1";                  // quantity starts in 1 by default
  String txtAction, txtConfirm;
  int operId;
  GUIOperation myContainer;

  /**
   * Constructs a new instance.
   * @param parent
   * @param title
   * @param modal
   */
  public GUIQuantifier( GUIOperation pContainer, String title, boolean modal )
  {
    super( pContainer.myContainer.mcdMain.getFrame(), title, modal );
    try
    {
      myContainer = pContainer;
      txtAction = Texts.QTR_QUANTITY + Texts.APN_ACTIONS[pContainer.getType()].toLowerCase();
      txtConfirm = Texts.QTR_CONFIRM + Texts.APN_OPERATIONS[pContainer.getType()].toLowerCase()
          + Texts.QTR_CONFIRM2;
      jbInit();
      pack();
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception
  {
    // sets panels
    jPanelBase.setLayout( layoutBase );
    jPanelBase.setPreferredSize( McdPrs.dialogSize );
    jPanelQties.setLayout( layoutQties );
    jPanelOptions.setLayout( layoutOptions );
    jPanelBase.setBackground( Colors.QTR_BACK );
    jPanelQties.setBackground( Colors.QTR_BACK );

    // sets labels
    jLabelTitle.setText( "Product Name" );
    jLabelTitle.setFont( Texts.fntTitle );
    jLabelTitle.setHorizontalAlignment( SwingConstants.CENTER );
    jLabelTitle.setForeground( Colors.APN_TEXT );
    jLabelQty.setText( txtAction );
    jLabelQty.setFont( Texts.fntText );
    jLabelQty.setHorizontalAlignment( SwingConstants.CENTER );
    jLabelQty.setBorder( BorderFactory.createMatteBorder( 1, 0, 1, 0, Colors.QTR_BORDER ) );
    jLabelQty.setForeground( Colors.APN_TEXT );
    this.setQuantity( quantity );
    jLabelConf.setFont( Texts.fntText );
    jLabelConf.setHorizontalAlignment( SwingConstants.CENTER );
    jLabelConf.setBorder( BorderFactory.createMatteBorder( 1, 0, 1, 0, Colors.QTR_BORDER ) );
    jLabelConf.setForeground( Colors.APN_TEXT );

    // numeric buttons
    // creates buttons
    jButton1 = new GUIButtonImage( "1", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton2 = new GUIButtonImage( "2", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton3 = new GUIButtonImage( "3", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton4 = new GUIButtonImage( "4", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton5 = new GUIButtonImage( "5", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton6 = new GUIButtonImage( "6", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton7 = new GUIButtonImage( "7", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton8 = new GUIButtonImage( "8", Images.buttCalcKey, Images.buttCalcKeyDn );
    jButton9 = new GUIButtonImage( "9", Images.buttCalcKey, Images.buttCalcKeyDn );
    // sets font type
    jButton1.setFont( Texts.fntKey );
    jButton2.setFont( Texts.fntKey );
    jButton3.setFont( Texts.fntKey );
    jButton4.setFont( Texts.fntKey );
    jButton5.setFont( Texts.fntKey );
    jButton6.setFont( Texts.fntKey );
    jButton7.setFont( Texts.fntKey );
    jButton8.setFont( Texts.fntKey );
    jButton9.setFont( Texts.fntKey );
    // sets font color
    jButton1.setForeground( Colors.KEY_TEXT );
    jButton2.setForeground( Colors.KEY_TEXT );
    jButton3.setForeground( Colors.KEY_TEXT );
    jButton4.setForeground( Colors.KEY_TEXT );
    jButton5.setForeground( Colors.KEY_TEXT );
    jButton6.setForeground( Colors.KEY_TEXT );
    jButton7.setForeground( Colors.KEY_TEXT );
    jButton8.setForeground( Colors.KEY_TEXT );
    jButton9.setForeground( Colors.KEY_TEXT );
    // sets acctions
    jButton1.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton2.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton3.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton4.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton5.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton6.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton7.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton8.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );
    jButton9.addActionListener( new GUIQuantifier_jButton_actionAdapter( this ) );

    // option buttons
    jButtCancel = new GUIButtonImage( Images.buttCalcCancel, Images.buttCalcCancelDn );
    jButtCancel.addActionListener( new GUIQuantifier_jButtCancel_actionAdapter( this ) );
    jButtAcept = new GUIButtonImage( Images.buttCalcAcept, Images.buttCalcAceptDn );
    jButtAcept.addActionListener( new GUIQuantifier_jButtAcept_actionAdapter( this ) );

    getContentPane().add( jPanelBase );
    jPanelBase.add( jLabelTitle, new GridBagConstraints2( 0, 0, 2, 1, 1.0, 0.0,
        GridBagConstraints.NORTH, GridBagConstraints.BOTH, Texts.zeroMargin, 0, 0 ) );
    jPanelBase.add( jLabelQty, new GridBagConstraints2( 0, 1, 2, 1, 1.0, 0.0,
        GridBagConstraints.NORTH, GridBagConstraints.BOTH, Texts.zeroMargin, 0, 0 ) );
    jPanelBase.add( jPanelQties, new GridBagConstraints2( 0, 2, 2, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );
    jPanelBase.add( jLabelConf, new GridBagConstraints2( 0, 3, 2, 1, 1.0, 0.0,
        GridBagConstraints.SOUTH, GridBagConstraints.BOTH, Texts.zeroMargin, 0, 0 ) );
    jPanelBase.add( jButtCancel, new GridBagConstraints2( 0, 4, 1, 1, 1.0, 0.0,
        GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );
    jPanelBase.add( jButtAcept, new GridBagConstraints2( 1, 4, 1, 1, 1.0, 0.0,
        GridBagConstraints.WEST, GridBagConstraints.NONE, Texts.dialogMargin, 0, 0 ) );

    // adds buttons
    jPanelQties.add( jButton1 );
    jPanelQties.add( jButton2 );
    jPanelQties.add( jButton3 );
    jPanelQties.add( jButton4 );
    jPanelQties.add( jButton5 );
    jPanelQties.add( jButton6 );
    jPanelQties.add( jButton7 );
    jPanelQties.add( jButton8 );
    jPanelQties.add( jButton9 );
  }

  /**
   * Sets selected product quantity
   * @param pQty quantity selected
   */
  public void setQuantity( String pQty )
  {
    this.jLabelConf.setText( txtConfirm + pQty
        + ( ( Integer.parseInt( pQty ) > 1 )? Texts.CFR_UNITS : Texts.CFR_UNIT ) + Texts.CFR_FORSURE_END );
  }

  /**
   * Sets product for quantity selection
   * @param pProd Product instance selected
   * @param pAll indicates if buttons must be all active or only those permited by quantity
   */
  public void setProduct( Product pProd, boolean pAll )
  {
    int li_qty = 0;
    product = pProd;
    jLabelTitle.setText( product.getDescription() );
    if ( !pAll )
    {
      li_qty = (product.getMaxQty() > 0)? Math.min( product.getQuantity(), product.getMaxQty() )
          : product.getQuantity();
      switch ( li_qty )
      {
        case 1:
          jButton2.setDisabledIcon( Images.buttCalcKeyOff );
          jButton2.setEnabled( false );
        case 2:
          jButton3.setDisabledIcon( Images.buttCalcKeyOff );
          jButton3.setEnabled( false );
        case 3:
          jButton4.setDisabledIcon( Images.buttCalcKeyOff );
          jButton4.setEnabled( false );
        case 4:
          jButton5.setDisabledIcon( Images.buttCalcKeyOff );
          jButton5.setEnabled( false );
        case 5:
          jButton6.setDisabledIcon( Images.buttCalcKeyOff );
          jButton6.setEnabled( false );
        case 6:
          jButton7.setDisabledIcon( Images.buttCalcKeyOff );
          jButton7.setEnabled( false );
        case 7:
          jButton8.setDisabledIcon( Images.buttCalcKeyOff );
          jButton8.setEnabled( false );
        case 8:
          jButton9.setDisabledIcon( Images.buttCalcKeyOff );
          jButton9.setEnabled( false );
      }
    }
  }

  /**
   * Returns selected value
   * @return String selected value
   */
  public String getSelection()
  {
    return quantity;
  }

  void jButtCancel_actionPerformed( ActionEvent e )
  {
    quantity = "0";
    this.stop();
  }

  void jButtAcept_actionPerformed( ActionEvent e )
  {
    this.stop();
  }

  void jButton_actionPerformed( ActionEvent e )
  {
    int li_maxQty,      // quantity available
        li_selQty;      // quantity selected

    this.kickTimer();
    JButton l_butt = (JButton) e.getSource();
    li_selQty = Integer.parseInt( l_butt.getText() );
    quantity = "" + li_selQty;

    setQuantity( quantity );
  }
}

class GUIQuantifier_jButtCancel_actionAdapter implements java.awt.event.ActionListener
{
  GUIQuantifier adaptee;

  GUIQuantifier_jButtCancel_actionAdapter( GUIQuantifier adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtCancel_actionPerformed( e );
  }
}

class GUIQuantifier_jButtAcept_actionAdapter implements java.awt.event.ActionListener
{
  GUIQuantifier adaptee;

  GUIQuantifier_jButtAcept_actionAdapter( GUIQuantifier adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButtAcept_actionPerformed( e );
  }
}

class GUIQuantifier_jButton_actionAdapter implements java.awt.event.ActionListener{
  GUIQuantifier adaptee;

  GUIQuantifier_jButton_actionAdapter( GUIQuantifier adaptee )
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed( ActionEvent e )
  {
    adaptee.jButton_actionPerformed( e );
  }
}
